package com.system.Tools;

import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class TimeTools {
    /**
     * 获取当前时间
     *
     * @return
     */
    public String getTime() {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(date);
    }

    public String getTimeInt() {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        return formatter.format(date);
    }
    /**
     * 获取年月日
     *
     * @return
     */
    public String getTimeYMD() {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(date);
    }

    /**
     * 获取精确到毫秒的时间戳
     *
     * @param
     * @return
     **/
    public Long getTimestamp() {
        Date date = new Date();
        String timestamp = String.valueOf(date.getTime());
        return Long.valueOf(timestamp);
    }

    public List<String> getTimeArr(String time, Integer length) {
        List<String> timeList = new ArrayList<>();
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = simpleDateFormat.parse(time);
            for (int i = 0; i < length; i++) {
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                cal.add(Calendar.DATE, i);
                String timeString = simpleDateFormat.format(cal.getTime());
                timeList.add(timeString);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeList;
    }

    //计较日期大小
    public String isOverdue(String time, Integer length) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = simpleDateFormat.parse(time);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.DATE, length);

            //获取剩余10天的
            Calendar cal10 = Calendar.getInstance();
            cal10.setTime(date);
            cal10.add(Calendar.DATE, (length-10));

            //保质期最后时间
            String timeString = simpleDateFormat.format(cal.getTime());
            Date dateTime = simpleDateFormat.parse(timeString);
            String timestamp = String.valueOf(dateTime.getTime());
            Long aLong = Long.valueOf(timestamp);

            //剩余10天
            //保质期最后时间
            String timeString10 = simpleDateFormat.format(cal10.getTime());
            Date dateTime10 = simpleDateFormat.parse(timeString10);
            String timestamp10 = String.valueOf(dateTime10.getTime());
            Long aLong10 = Long.valueOf(timestamp10);

            //现在时间
            Date dateNow = new Date();
            String timestampNow = String.valueOf(dateNow.getTime());
            Long aLongNow = Long.valueOf(timestampNow);

            String value = "";
            if(aLong > aLongNow){
                value= "剩余："+(aLong - aLongNow) / 1000 / (60 * 60 * 24) + "天";
            }else{
                value = "过期";
            }
            return value;


            /*if(aLongNow > aLong){
                return "过期";
            }
            if(aLong10 <= aLongNow && aLongNow <= aLong){
                return "临期";
            }

            if(aLong10 > aLongNow ){
                return "正常";
            }*/

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "质保状态异常";
    }


}
